package com.tfdy.util.geo;

/**
 * @author:LuxingZhou
 * @description:
 * @date:Created in2018/6/21  11:07
 */

import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

public class CoordinateConversion02 {

    /**
     * 平面坐标转经纬度
     */
    public static double[] convert(double x, double y,CoordinateReferenceSystem crs)
            throws FactoryException, MismatchedDimensionException, TransformException {
        //Coordinate sourceCoord = new Coordinate(x, y);
        //GeometryFactory geoFactory = new GeometryFactory();
        //Point sourcePoint = geoFactory.createPoint(sourceCoord);

        org.locationtech.jts.geom.GeometryFactory geoFactory = new org.locationtech.jts.geom.GeometryFactory();
        //geometry.createGeometry()
        Coordinate coordinate = new Coordinate(x,y);
        Geometry geometry = geoFactory.createPoint(coordinate).reverse();
        MathTransform transform = CRS.findMathTransform(crs, DefaultGeographicCRS.WGS84, false);
        Geometry targetGeometry = JTS.transform(geometry, transform);
        Point targetPoint = (Point)targetGeometry;
        double[] targetCoord = {targetPoint.getX(), targetPoint.getY()};
        return targetCoord;
    }
    /**
     * 测试
     */
    public static void main( String[] args ) throws Exception
    {
        double x = -132202.486;
        double y = 1657014.274;
        //第一种获取方式，imageFile为卫星影像的文件地址，目的是获取影像的投影
//        GeoTiffReader reader = new GeoTiffReader(imageFile);
//        GridCoverage2D coverage = reader.read(null);
//        //获取投影
//        CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem2D();
        //这是第二种获取方式（参考博文），
        final String wkt="PROJCS[\"unnamed\","
                +"GEOGCS[\"WGS 84\","
                + "DATUM[\"World Geodetic System 1984\", "
                +"SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]],"
                + "AUTHORITY[\"EPSG\",\"6326\"]], "
                +  "PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]],"
                + " UNIT[\"degree\", 0.017453292519943295], "
                + "AXIS[\"Geodetic longitude\", EAST], "
                +" AXIS[\"Geodetic latitude\", NORTH], "
                + "AUTHORITY[\"EPSG\",\"4326\"]], "
                + " PROJECTION[\"Albers_Conic_Equal_Area\"], "
                +  "PARAMETER[\"central_meridian\", 110.0], "
                + " PARAMETER[\"latitude_of_origin\", 12.0], "
                +" PARAMETER[\"standard_parallel_1\", 25.0], "
                +  "PARAMETER[\"false_easting\", 0.0], "
                +  "PARAMETER[\"false_northing\", 0.0],"
                + "PARAMETER[\"standard_parallel_2\", 47.0],"
                + "UNIT[\"m\", 1.0], "
                + "AXIS[\"Easting\", EAST], "
                +  "AXIS[\"Northing\", NORTH]]";
        CoordinateReferenceSystem crs = CRS.parseWKT(wkt);
        double[] coordinate = convert(x, y, crs);
        System.out.println("X: " + coordinate[0] + ", Y: " + coordinate[1]);
    }
}
